<template>
  <view class="pl-[24rpx] pr-[24rpx]">
    <custom-navbar title="个人信息" :autoBack="true" leftIconColor="#303133" />
    <up-gap height="66rpx" />
    <infocard :userInfoList="userInfoList" />
    <up-gap height="66rpx" />
    <view v-if="clientType === 'customer'">
      <infocard :userInfoList="userInfoListNext" />
      <up-gap height="66rpx" />
      <infocard :userInfoList="userInfoListOther" />
    </view>
    <view v-else>
      <infocard :userInfoList="userInfoListNextEnterprise" />
      <up-gap height="66rpx" />
      <infocard :userInfoList="userInfoListOtherEnterprise" />
      <up-gap height="66rpx" />
      <infocard :userInfoList="userInfoListLastEnterprise" />
    </view>
    <view class="flex items-center justify-center mt-10">
      <view
        class="mx-auto my-0 mt-_a_24rpx_a_ h-82rpx w-_a_90_a__a_ rounded-_a_42rpx_a_ bg-_a_1f50f2 text-center text-_a_30rpx_a_ text-white font-medium leading-_a_82rpx_a_"
        @click="updateInfo">更新用户信息</view>
    </view>
  </view>
</template>
<script lang="ts" setup>
  import { userUserInfo, updateUserInfo } from "@/api/user";
  import infocard from "@/pages/components/infocard/index.vue";
  // import headerImg from "@/static/images/tab/user-header.png";
  import { useUserStore } from "@/store";
  import { commonAddress } from '@/api/common/index'
  const headerImg = 'https://yiliao.test03.qcw800.com/static/images/tab/user-header.png'
  const userInfoList = reactive([
    {
      title: "头像",
      value: '',
      valueType: 'img',
      rigthIco: true,
    },
    {
      title: "真实姓名",
      value: "",
      valueType: 'input',
      rigthIco: true,
    },
    {
      title: "性别",
      value: "请选择",
      label: '请选择',
      valueType: 'select',
      rigthIco: true,
      selectOptions: [
        {
          label: "男",
          value: "男",
        },
        {
          label: "女",
          value: "女",
        }
      ],
    }])
  const userInfoListNext = reactive([
    {
      title: "手机号",
      value: "",
      valueType: 'input',
      rigthIco: true,
    },
    {
      title: "邮箱",
      value: "",
      valueType: 'input',
      rigthIco: true,
    },
    {
      title: "生日",
      value: "请选择",
      valueType: 'date',
      rigthIco: true,
    }
  ])
  const userInfoListOther = reactive([
    {
      title: "籍贯",
      value: "请选择",
      label: '请选择',
      valueType: 'city',
      rigthIco: true,
    },
    {
      title: "政治面貌",
      value: "请选择",
      label: '请选择',
      valueType: 'select',
      rigthIco: true,
    }
  ])


  const userInfoListNextEnterprise = reactive([
    {
      title: "所在公司",
      value: "请选择",
      rigthIco: true,
      valueType: "text",
      options: [["CEO", "董事长", "秘书"]],
    },
    {
      title: "职位",
      value: "请选择",
      rigthIco: true,
      valueType: "text",
      options: [["CEO", "董事长", "秘书"]],
    },
    {
      title: "工作地区",
      value: "请选择",
      rigthIco: true,
      valueType: "text",
      options: [["CEO", "董事长", "秘书"]],
    },
    {
      title: "自我介绍",
      value: "请选择",
      rigthIco: true,
      valueType: "text",
      options: [["CEO", "董事长", "秘书"]],
    },
  ])

  const userInfoListOtherEnterprise = reactive([
    {
      title: "手机",
      value: "2133266955",
      rigthIco: true,
      valueType: "input",
    },
    {
      title: "邮箱",
      value: "M122311313@123.com",
      rigthIco: true,
      valueType: "input",
    },
  ])

  const userInfoListLastEnterprise = reactive([
    {
      title: "处理简历时间",
      value: "请选择",
      rigthIco: true,
      valueType: "text",
      options: [["CEO", "董事长", "秘书"]],
    }
  ])
  const clientType = ref('')

  const getUserInfo = async () => {
    try {
      const result = await userUserInfo();
      console.log('我的-用户详情', result);
      if (!result) {
        return {}
      }
      let r = {
        avatar: result.avatar,
        name: result.name,
        phone: result.phone,
        email: result.email,
        born: result.born,
        gender: result.gender,
        province_id: result.province_id,
        city_id: result.city_id,
        province: result.province.name,
        city: result.city.name,
        political_outlook: result.political_outlook == 1 ? "普通公民" : result.political_outlook == 2 ? "预备党员" : "党员",
      }
      userInfoList[0].value = r.avatar
      userInfoList[1].value = r.name
      userInfoList[2].value = r.gender
      userInfoListNext[0].value = r.phone
      userInfoListNext[1].value = r.email
      userInfoListNext[2].value = r.born
      userInfoListOther[0].label = r.province + ' ' + r.city
      userInfoListOther[0].value = r.province_id + ',' + r.city_id
      userInfoListOther[1].label = r.political_outlook
      console.log('---籍贯', userInfoListOther);
    }
    catch (e) {
      console.log(e);
    }
  };
  const updateInfo = async () => {
    console.log('更新', userInfoList, userInfoListNext, userInfoListOther, userInfoListNextEnterprise, userInfoListOtherEnterprise)
    try {
      //用户端更新参数
      let params = {
      }
      if (userInfoList[0].value != "") {
        params.avatar = userInfoList[0].value
      } else {
        uni.showToast({
          title: '请上传头像',
          icon: 'none'
        })
        return
      }
      if (userInfoList[1].value != "") {
        params.name = userInfoList[1].value
      } else {
        uni.showToast({ title: '请填写真实姓名' })
        return
      }
      if (userInfoList[2].value != "请选择") {
        params.gender = userInfoList[2].value
      } else {
        uni.showToast({
          title: '请选择性别',
          icon: 'none'
        })
        return
      }
      if (userInfoListOther[0].value != "请选择") {
        params.province_id = userInfoListOther[0].value.split(',')[0]
        params.city_id = userInfoListOther[0].value.split(',')[1]
      } else {
        uni.showToast({
          title: '请选择籍贯',
          icon: 'none'
        })
        return
      }
      if (userInfoListOther[1].value != "请选择") {
        params.political_outlook = userInfoListOther[1].value
      } else {
        uni.showToast({
          title: '请选择政治面貌',
          icon: 'none'
        })
        return
      }
      if (userInfoListNext[0].value != "") {
        params.phone = userInfoListNext[0].value
      } else {
        uni.showToast({
          title: '请填写手机号',
          icon: 'none'
        })
        return
      }
      if (userInfoListNext[1].value != "") {
        params.email = userInfoListNext[1].value
      } else {
        uni.showToast({
          title: '请填写邮箱',
          icon: 'none'
        })
        return
      }
      if (userInfoListNext[2].value != "") {
        params.born = userInfoListNext[2].value
      } else {
        uni.showToast({
          title: '请填写生日',
          icon: 'none'
        })
        return
      }
      if (Object.keys(params).length == 0) {
        uni.showToast({
          title: '请修改需要更新的信息',
          icon: 'none'
        })
        return
      }
      console.log("更新用户信息参数:::", params)
      let result = await updateUserInfo(params);
      if (result) {
        uni.showToast({
          title: '更新成功',
          icon: 'none'
        })
      } else {
        uni.showToast({
          title: '更新失败',
          icon: 'none'
        })
      }
    } catch (e) {
      console.log("更新用户信息 err:::", e);
      uni.showToast({
        title: '更新失败',
        icon: 'none'
      })
    }
  }
  // 获取城市
  onLoad(() => {
    getUserInfo()
    let value = JSON.parse(uni.getStorageSync('config')).configCli
    userInfoListOther[1].selectOptions = value.config_political_outlook$$政治面貌.map(item => {
      return {
        label: item.value,
        value: item.key
      }
    })
  })
  onShow(() => {
    console.log('进来了-----------------')
    clientType.value = uni.getStorageSync('clientType');
    console.log('===========', clientType.value)
  })
</script>
